import { Component, Inject } from '@angular/core';
import { IonicPage, ModalController, ViewController } from 'ionic-angular';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from "rxjs/Observable";

// 常量
// import * as PageConstant from '../../../constant/pageConstant';
// import * as Constant from '../../../constant/constant';

// service
import { Auth, User } from '../../../providers/entity/user.model';
import { LoadingService } from '../../../providers/utils/loading.service';
/**
 * 运营商授权
 */
@IonicPage()
@Component({
  selector: 'page-telephone-auth',
  templateUrl: 'telephone-auth.html',
})
export class TelephoneAuthPage {

  // 是否同意协议
  private hasActive: boolean= true;
  // 服务密码
  protected pwd: any= '';
  // 短信验证码
  protected msgCode: any= '';
  // 是否显示密码
  private switchover: any = {
    type: 'password',
    status: true
  };
  // 用户信息
  private userInfo: any;
  // 监听 用户信息
  private userSubscription: Subscription;
  constructor(
    public modalCtrl: ModalController,
    public loadingCtrl: LoadingService,
    public viewCtrl: ViewController,
    @Inject('user') private userService,
    @Inject('auth') private authService,
  )
  {
    this.userSubscription=
      Observable.combineLatest(this.authService.getAuth(),
        this.userService.getUser(),(auth: Auth, user: User)=>{
          const username = auth['login'].username; // 验证登录 也可从user拿到
          const { idInfo, mobileInfo }= user;
          return { username, idInfo, mobileInfo };
        })
        .subscribe((data)=>{
          debugger;
          const { idNo , realname: name }= data.idInfo;
          const {  token, collect_website }= data.mobileInfo;
          const phone= data.username;
          this.userInfo= { name, idNo, phone, token, collect_website };
          if(!token){ //token不存在,默认为第一步
            this.userService.getMobileAuthToken(this.userInfo);
          }
        });
  }

  ionViewDidLoad() {}

  /*** 页面进入
   * */
  ionViewWillEnter() {}

  /*** 页面离开
   * */
  ionViewWillLeave()
  {

    if(typeof this.userSubscription !== 'undefined')
      this.userSubscription.unsubscribe();
  }

  /*** 切换密码显示方式
   * */
  public switchoverPassword(obj: any): void
  {
    if(!obj.type) return;
    this.switchover = {
      type: obj.type==='text'? 'password': 'text',
      status: !obj.status
    }

  }

  /*** 切换密码显示方式
   * */
  public clearMsgCode(): void
  {
    this.msgCode = !this.msgCode;
  }

  /*** 切换是否同意
   * */
  executeChange()
  {
    this.hasActive = !this.hasActive;
  }

  /*** 去服务协议页面
   * */
  doServiceAgreement()
  {
    /*let modal = this.modalCtrl.create(PageConstant.ServiceAgreementPage, {
      data: Constant.TELEPHONE_PROTOCOL
    },{
      showBackdrop: false,
      enableBackdropDismiss: false,
    });
    modal.present();*/
  }
  /*** 验证登录
   * */
  executeMobileLogin(){
    this.loadingCtrl.createLoading('加载中');
    this.userService.MobileLogin(
      {...this.userInfo, pwd: this.pwd}, this.viewCtrl );
  }

  /*** 保存数据
  * */
  /*executeSave(){
    this.loadingCtrl.createLoading('加载中');
    this.userService.mobileDataSave(
      {...this.userInfo, captcha: this.msgCode}, this.viewCtrl );
  }*/

}
